<?php if( ! defined('ABSPATH')) exit('restricted access');

/**
 * WPnukes Admin Panel
 *
 * An open source wordpress themes options admin panel
 *
 * @package		WPnukes Apanel
 * @author		WPnukes team <info@wpnukes.com>
 * @copyright	Copyright 2012 © Fourthwave technologies, (PVT) Limited. - All rights reserved
 * @license		http://wpnukes.com/admin_panel/license.html
 * @website		http://wpnukes.com
 * @since		Version 1.0
 */

class doctors_List_Table extends WP_List_Table
{	
    var $_args = array('role'=>'doctors','number'=>'5', 'singular'=>'doctor_list_table', 'plural' =>'doctors_list_table','ajax'=>false, 'screen'=>null);
	private $text_domain = '';
	
	function __construct($text_domain)
	{	
		$this->text_domain = $text_domain;
		
		$this->screen = convert_to_screen( $this->_args['screen'] );

		echo '<div class="wrap">
				<div class="icon32 icon32-posts-post" id="icon-edit"><br>
			</div><h2>'.__('Find a Doctor', $this->text_domain).'<a class="add-new-h2" href="'.add_query_arg(array('role' => 'doctors'), 'user-new.php').'">'.__('Add New', $this->text_domain).'</a></h2>';
			
		$this->prepare_items();
		
		echo '<form method="post"><input type="hidden" name="page" value="nukes_doctors" />';
		    $this->search_box('search', 'search_id');
		echo '</form>';
		
		$this->display();
		echo '</div>'; 
	}
	
	/** Prepare Items */
    function prepare_items()
	{
		global $wpdb, $_wp_column_headers;
		
		$screen = get_current_screen();
		
		/** Get Columns for top and bottom */
		$columns = $this->get_columns();
		
		/** Hidden Columns List */
		$hidden = array();

		/** Sortable Columns list */
		$sortable = $this->get_sortable_columns();
		$this->_column_headers = array($columns, $hidden, $sortable);

		$this->query_filters();
		$doctors = new WP_User_Query( $this->_args );

		$this->items = array();
		foreach($doctors->get_results() as $doctor)
		{
			foreach(get_user_meta($doctor->ID) as $k=>$v)
				$doctor->$k = $v[0];
			$appointment = $wpdb->get_row("SELECT COUNT(ID) as appointment FROM ".$wpdb->prefix."appointments WHERE requested_phy = ".$doctor->ID);
			$doctor->appointment = $appointment->appointment;
			$this->items[] = $doctor;
		}
		
		/** Pagination Support */
		$this->set_pagination_args(array(
											'total_items' => $doctors->total_users, //WE have to calculate the total number of items
											'per_page' => $this->_args['number'] //WE have to determine how many items to show on a page
											)
									);
    }
	
	/** Default Column view */
	/** Wordpress looks for column_COLUMNNAME for custom styling */
    function column_default($item, $column_name)
	{
		return $item->$column_name;
    }
	
	/** Sortable Columns list */
    function get_sortable_columns()
	{
		return array(
						'display_name'=>array( __('Name', $this->text_domain), false),
						'major'=>array( __('Major', $this->text_domain) ,false),
						'gender'=>array( __('Gender', $this->text_domain) ,false),
						'city'=>array( __('City', $this->text_domain) ,false),
						'state'=>array( __('State', $this->text_domain) , false),
					);
    }
	
	/** Search support */
    function query_filters()
	{
		/** Search support */
		if(isset($_POST['s']))
		{
			$this->_args['search'] = $_POST['s'];
			$this->_args['meta_compare'] = 'LIKE';		
			$this->_args['meta_value'] = $_POST['s'];
		}

		/** If no sort, default to title */
		$this->_args['orderby'] = ( ! empty( $_GET['orderby'] ) ) ? $_GET['orderby'] : 'display_name';
		
		/** If no order, default to asc */
		$this->_args['order'] = ( ! empty($_GET['order'] ) ) ? $_GET['order'] : 'asc';
		
		/** Offset */
		$this->_args['offset'] = ($this->get_pagenum() - 1) * $this->_args['number'];
    }
	
	/** Custom column view */
    function column_display_name($item)
	{
		$actions = array(
						'edit' => __(sprintf('<a href="'.admin_url('user-edit.php?user_id=%s').'">Edit</a>',$item->ID), $this->text_domain),
						'delete' => "<a class='submitdelete' href='" . wp_nonce_url( "users.php?action=delete&amp;role=doctors&amp;user=".$item->ID."", 'bulk-users' ) . "'>" . __( 'Delete', $this->text_domain ) . "</a>",
					);
					
		return sprintf('%1$s %2$s', '<strong>'.$item->display_name.'</strong>', $this->row_actions($actions) );
    }

	/** Bulk Action Support */
    function get_bulk_actions()
	{
		return array();
		$actions = array('delete' => __('Delete', $this->text_domain));
		return $actions;
    }
	
	/** Get the columns list */
	function get_columns()
	{
		return array('cb' => '<input type="checkbox" />',
					'display_name'=> __('Name', $this->text_domain),
					'major'=> __('Major', $this->text_domain),
					'gender'=> __('Gender', $this->text_domain),
					'city'=> __('City', $this->text_domain),
					'state'=> __('State', $this->text_domain),
					'appointment'=> __('Appointments', $this->text_domain)
					);
    }
	
	/** Checkbox support */
    function column_cb($item)
	{
		return sprintf(
		'<input type="checkbox" name="doctors[]" value="%s" />', $item->ID
		);
    }
	
	/** No item message */
    function no_items()
	{
    	_e('No doctors found', $this->text_domain);
    }
}